package com.qj.daibei.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qj.daibei.mapper.LoanMapper;
import com.qj.daibei.pojo.Loan;
import com.qj.daibei.service.LoanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * author wml
 * Created on 2021/10/6 15:34
 */
@Service
public class LoanServiceImpl implements LoanService {

    @Autowired
    private LoanMapper loanMapper;

    @Override
    public int insertLoan(Loan loan) {
        return this.loanMapper.insert(loan);
    }

    /**
     * author kxz
     * createTime on 2021/10/15
     * @param userId
     * @return
     */
    @Override
    public int queryForRecordByUserId(Integer userId) {
        int loanAll = 0;
        QueryWrapper<Loan> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("money").eq("user_id", userId);
        List<Object> moneyList = loanMapper.selectObjs(queryWrapper);
        for(int i=0;i<moneyList.size();i++){
            if(moneyList.get(i)==null){
                continue;
            }
            loanAll+=(int) moneyList.get(i);
        }
        return loanAll;
    }

    @Override
    public List<Loan> findLoanByUserId(Integer userId) {
        return this.loanMapper.findLoanByUserId(userId);
    }

    @Override
    public List<Loan> findLoanByUserIdByPageFirst(Integer userId, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Loan> loanList = this.loanMapper.findLoanByUserId(userId);
        return loanList;
    }

    @Override
    public PageInfo<Loan> findLoanByUserIdByPageSecond(Integer userId, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Loan> loanList = this.loanMapper.findLoanByUserId(userId);
        PageInfo<Loan> pageInfo = new PageInfo<>(loanList);
        return pageInfo;
    }

    @Override
    public List<Object> findLoanIdByUserId(Integer userId) {
        QueryWrapper<Loan> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("loan_id").eq("user_id", userId);
        return this.loanMapper.selectObjs(queryWrapper);
    }
}
